package com.demo.controller;

import com.demo.common.Result;
import com.demo.mapper.TimeSlotMapper;
import com.demo.pojo.entity.TimeSlot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 时间段管理控制器
 */
@RestController
@RequestMapping("/timeslot")
@CrossOrigin
public class TimeSlotController {
    
    @Autowired
    private TimeSlotMapper timeSlotMapper;
    
    /**
     * 为指定学期添加时间段数据
     */
    @PostMapping("/add-semester/{semester}")
    public Result<String> addSemesterTimeSlots(@PathVariable String semester) {
        try {
            System.out.println("开始为学期 " + semester + " 添加时间段数据");
            
            // 检查是否已存在该学期的时间段数据
            List<TimeSlot> existingSlots = timeSlotMapper.selectList(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<TimeSlot>()
                    .eq("semester", semester)
            );
            
            if (!existingSlots.isEmpty()) {
                return Result.error("学期 " + semester + " 的时间段数据已存在，共 " + existingSlots.size() + " 条记录");
            }
            
            // 创建时间段数据
            List<TimeSlot> timeSlots = createTimeSlotsForSemester(semester);
            
            // 批量插入
            int successCount = 0;
            for (TimeSlot timeSlot : timeSlots) {
                try {
                    timeSlotMapper.insert(timeSlot);
                    successCount++;
                } catch (Exception e) {
                    System.err.println("插入时间段失败：" + e.getMessage());
                }
            }
            
            System.out.println("成功为学期 " + semester + " 添加 " + successCount + " 个时间段");
            return Result.success("成功为学期 " + semester + " 添加 " + successCount + " 个时间段");
            
        } catch (Exception e) {
            System.err.println("添加学期时间段数据失败：" + e.getMessage());
            e.printStackTrace();
            return Result.error("添加学期时间段数据失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取指定学期的时间段数据
     */
    @GetMapping("/semester/{semester}")
    public Result<List<TimeSlot>> getSemesterTimeSlots(@PathVariable String semester) {
        try {
            List<TimeSlot> timeSlots = timeSlotMapper.selectList(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<TimeSlot>()
                    .eq("semester", semester)
            );
            return Result.success(timeSlots);
        } catch (Exception e) {
            return Result.error("获取学期时间段数据失败：" + e.getMessage());
        }
    }
    
    /**
     * 创建指定学期的时间段数据
     */
    private List<TimeSlot> createTimeSlotsForSemester(String semester) {
        List<TimeSlot> timeSlots = new ArrayList<>();
        
        // 哈尔滨信息工程学院作息时间
        LocalTime[] startTimes = {
            LocalTime.of(8, 0), LocalTime.of(8, 55), LocalTime.of(10, 0), LocalTime.of(10, 55),
            LocalTime.of(14, 0), LocalTime.of(14, 55), LocalTime.of(16, 0), LocalTime.of(16, 55)
        };
        
        LocalTime[] endTimes = {
            LocalTime.of(8, 45), LocalTime.of(9, 40), LocalTime.of(10, 45), LocalTime.of(11, 40),
            LocalTime.of(14, 45), LocalTime.of(15, 40), LocalTime.of(16, 45), LocalTime.of(17, 40)
        };
        
        String[] remarks = {
            "第1节课", "第2节课", "第3节课", "第4节课",
            "第5节课", "第6节课", "第7节课", "第8节课"
        };
        
        // 为第1周创建时间段（周一到周五）
        for (int dayOfWeek = 1; dayOfWeek <= 5; dayOfWeek++) {
            for (int period = 1; period <= 8; period++) {
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setSemester(semester);
                timeSlot.setWeekNo(1);
                timeSlot.setDayOfWeek(dayOfWeek);
                timeSlot.setPeriodNo(period);
                timeSlot.setStartTime(startTimes[period - 1]);
                timeSlot.setEndTime(endTimes[period - 1]);
                timeSlot.setTimeType("正常");
                timeSlot.setStatus(1);
                timeSlot.setTimeRemark(remarks[period - 1]);
                timeSlot.setCreateTime(new Date());
                timeSlot.setUpdateTime(new Date());
                
                timeSlots.add(timeSlot);
            }
        }
        
        return timeSlots;
    }
}
























